package com.tudouji.framework.task;


import com.tudouji.common.utils.IdUtils;
import com.tudouji.project.bill.domain.Whinfo;
import com.tudouji.project.bill.domain.Whinforecord;
import com.tudouji.project.bill.service.impl.WhinfoServiceImpl;
import com.tudouji.project.bill.service.impl.WhinforecordServiceImpl;
import com.tudouji.project.repository.domain.RptStoreanalysis;
import com.tudouji.project.repository.service.impl.RptStoreanalysisServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 每日库存
 */
@Slf4j
@Component("whinfoRecordTask")
public class WhinfoRecordTask {

    @Autowired
    WhinfoServiceImpl whinfoService;
    @Autowired
    RptStoreanalysisServiceImpl rptStoreanalysisService;
    @Autowired
    WhinforecordServiceImpl whinforecordService;
    /**
     * 每日库存重跑
     * @Author shaodong
     * @Description
     * @Date 9:31 2021/1/6
     * @Param
     * @return
     **/
    public void handle(){
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,-24);
        String yesterdayDate=dateFormat.format(calendar.getTime());
        try {
            log.info("whinfoRecordTask.handle：凌晨跑昨天的每日库存");
            whinfoService.recoverWhinfo(yesterdayDate, yesterdayDate);
            log.info("whinfoRecordTask.handle：凌晨跑昨天的每日库存");
            log.info("whinfoRecordTask.handle：凌晨跑昨天的每日动态库存");
            rptStoreanalysisService.recoverRptAnalysis(yesterdayDate, yesterdayDate);
            log.info("whinfoRecordTask.handle：凌晨跑昨天的每日动态库存");
        } catch (Exception e) {
            log.info("凌晨跑昨天的每日库存失败：定时任务【whinfoRecordTask.handle】");
            log.info("异常信息：" + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 每日库存记录
     */
    @Transactional
    public void  winfoRecord() {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            winfoCalculate(simpleDate.parse(simpleDate.format(new Date())));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    /**
     * 动态库存记录
     */
    @Transactional
    public void rptAnalysis() {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        log.info("动态库存记录开始：" + simpleDate.format(date));
        rpt(new Date());
        log.info("动态库存记录结束：" + simpleDate.format(date));
    }

    public void rpt(Date date) {
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
        try {
            deleteToday(simpleDate.parse(simpleDate.format(date)));
            rptStoreanalysisService.rpt(simpleDate.parse(simpleDate.format(date)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存记录处理
     */
    public void winfoCalculate(Date toDay){
        try {
            /*--------------删除当天数据------------*/
            Whinforecord record = new Whinforecord();
            record.setRecorddate(toDay);
            List<Whinforecord> finds = whinforecordService.selectWhinforecordList(record);
            for (int i = 0; i < finds.size(); i++) {
                Whinforecord harvest = finds.get(i);
                if (harvest != null){
                    whinforecordService.deleteWhinforecordById(harvest.getGuid());
                }
            }
            /*--------------删除当天数据------------*/
//            whinforecordService.insertAllDay();
            List<Whinfo> whinfos = whinfoService.selectWhinfoList(new Whinfo());
            for (int i = 0; i < whinfos.size(); i++) {
                Whinfo wf = whinfos.get(i);
                if (wf != null){
                    Whinforecord newRecord = new Whinforecord();
                    newRecord.setStorermguid(wf.getStorermguid());
                    newRecord.setBatchid(wf.getBatchid());
                    newRecord.setStorehguid(wf.getStorehguid());
                    newRecord.setStorehcode(wf.getStorehcode());
                    newRecord.setProductid(wf.getProductid());
                    newRecord.setProductname(wf.getProductname());
                    newRecord.setStoreqty(wf.getStoreqty());
                    newRecord.setStockunitid(wf.getStockunitid());
                    newRecord.setStockunit(wf.getStockunit());
                    newRecord.setPrice(wf.getPrice());
                    newRecord.setWistartdate(wf.getWistartdate());
                    newRecord.setWienddate(wf.getWienddate());
                    newRecord.setWostartdate(wf.getWostartdate());
                    newRecord.setWoenddate(wf.getWoenddate());
                    newRecord.setCreatedate(wf.getCreatedate());
                    newRecord.setOperatorid(wf.getOperatorid());
//                    newRecord.setLastupdate(wf.getLastupdate());
                    newRecord.setOrgguid(wf.getOrgguid());
                    newRecord.setSpguid(wf.getSpguid());
                    newRecord.setRecorddate(toDay);
                    newRecord.setGuid(IdUtils.fastSimpleUUID());
                    whinforecordService.insertWhinforecord(newRecord);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除当天数据
     *
     * @param today
     * @throws Exception
     */
    public void deleteToday(Date today) throws Exception {
        RptStoreanalysis rpt = new RptStoreanalysis();
        rpt.setRptdate(today);
        List<RptStoreanalysis> rpts = rptStoreanalysisService.selectRptStoreanalysisList(rpt);
        for (RptStoreanalysis item : rpts) {
            rptStoreanalysisService.deleteRptStoreanalysisById(item.getGuid());
        }
    }
}
